<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryRef" :inline="true" v-show="showSearch" label-width="90">
      <el-form-item label="节奏型名称" prop="patternName">
        <el-input v-model="queryParams.patternName" placeholder="请输入节奏型名称" clearable @keyup.enter="handleQuery" />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
        <el-button icon="Refresh" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button type="primary" plain icon="Plus" @click="handleAdd"
          v-hasPermi="['system:pattern:add']">新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="success" plain icon="Edit" :disabled="single" @click="handleUpdate"
          v-hasPermi="['system:pattern:edit']">修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="danger" plain icon="Delete" :disabled="multiple" @click="handleDelete"
          v-hasPermi="['system:pattern:remove']">删除</el-button>
      </el-col>
      <right-toolbar v-model:showSearch="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="patternList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="节奏型名称" align="center" prop="patternName" />
      <el-table-column label="律动类型" align="center" prop="rhythm" />
      <el-table-column label="分类" align="center" prop="patternType" />
      <el-table-column label="技巧" align="center" prop="patternSkill" />
      <el-table-column label="节拍" align="center" prop="beat" />
      <el-table-column label="封面图片" align="center" prop="beat">
        <template #default="scope">
          <el-image v-if="scope.row.fileInfos" style="width: 100px; height: 100px" :src="scope.row.fileInfos[0]?.url" fit="cover" />
          <div v-else>暂无封面</div>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template #default="scope">
          <el-button link type="primary" icon="Edit" @click="handleUpdateJieZou(scope.row)">节奏上传</el-button>
          <el-button link type="primary" icon="Edit" @click="handleUpdate(scope.row)"
            v-hasPermi="['system:pattern:edit']">修改</el-button>
          <el-button link type="primary" icon="Delete" @click="handleDelete(scope.row)"
            v-hasPermi="['system:pattern:remove']">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination v-show="total > 0" :total="total" v-model:page="queryParams.pageNum"
      v-model:limit="queryParams.pageSize" @pagination="getList" />

    <!-- 添加或修改节奏型对话框 -->
    <el-dialog :title="title" v-model="open" width="500px" append-to-body>
      <el-form ref="patternRef" :model="form" :rules="rules" label-width="90px">
        <el-form-item label="节奏型名称" prop="patternName">
          <el-input v-model="form.patternName" placeholder="请输入节奏型名称" />
        </el-form-item>
        <el-form-item label="律动类型" prop="rhythm">
          <el-select style="width: 100%;" v-model="form.rhythm" placeholder="请选择律动类型">
            <el-option v-for="item in Dict_LD" :key="item.value" :label="item.label" :value="item.value" />
          </el-select>
        </el-form-item>
        <el-form-item label="分类" prop="patternType">
          <el-select style="width: 100%;" v-model="form.patternType" placeholder="请选择节拍">
            <el-option v-for="dict in pattern_type" :key="dict.value" :label="dict.label" :value="dict.value" />
          </el-select>
        </el-form-item>
        <el-form-item label="技巧" prop="patternSkill">
          <el-select style="width: 100%;" v-model="form.patternSkill" placeholder="请选择节拍">
            <el-option v-for="dict in pattern_skill" :key="dict.value" :label="dict.label" :value="dict.value" />
          </el-select>
        </el-form-item>
        <el-form-item label="节拍" prop="beat">
          <el-select style="width: 100%;" v-model="form.beat" placeholder="请选择节拍">
            <el-option v-for="dict in beat" :key="dict.value" :label="dict.label" :value="dict.value" />
          </el-select>
        </el-form-item>
        <el-form-item label="封面图片" prop="image">
          <ImageUpload v-model="imageList" :limit="1" />
        </el-form-item>
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button type="primary" @click="submitForm">确 定</el-button>
          <el-button @click="cancel">取 消</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script setup name="Pattern">
import { listPattern, getPattern, delPattern, addPattern, updatePattern } from "@/api/system/pattern";
import ImageUpload from "@/components/ImageUpload/index.vue";
import { Dict_LD } from "@/utils/dict";
import { ref, reactive, toRefs, getCurrentInstance } from "vue";
import { useRouter } from "vue-router";
const router = useRouter();
const { proxy } = getCurrentInstance();
const { pattern_skill, beat, pattern_type } = proxy.useDict("pattern_skill", "beat", "pattern_type");

const patternList = ref([]);
const open = ref(false);
const loading = ref(true);
const showSearch = ref(true);
const ids = ref([]);
const single = ref(true);
const multiple = ref(true);
const total = ref(0);
const title = ref("");
const imageList = ref([]);

const data = reactive({
  form: {
  },
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    patternName: null,
  },
  rules: {
  }
});

const { queryParams, form, rules } = toRefs(data);

/** 查询节奏型列表 */
function getList() {
  loading.value = true;
  listPattern(queryParams.value).then(response => {
    patternList.value = response.rows;
    total.value = response.total;
    loading.value = false;
  });
}

// 取消按钮
function cancel() {
  open.value = false;
  reset();
}

// 表单重置
function reset() {
  form.value = {
  };
  imageList.value = [];
  proxy.resetForm("patternRef");
}

/** 搜索按钮操作 */
function handleQuery() {
  queryParams.value.pageNum = 1;
  getList();
}

/** 重置按钮操作 */
function resetQuery() {
  proxy.resetForm("queryRef");
  handleQuery();
}

// 多选框选中数据
function handleSelectionChange(selection) {
  ids.value = selection.map(item => item.patternId);
  single.value = selection.length != 1;
  multiple.value = !selection.length;
}

/** 新增按钮操作 */
function handleAdd() {
  reset();
  open.value = true;
  title.value = "添加节奏型";
}

/** 修改按钮操作 */
function handleUpdate(row) {
  reset();
  const _patternId = row.patternId || ids.value
  getPattern(_patternId).then(response => {
    form.value = response.data;
    if (response.data.fileInfos && response.data.fileInfos.length > 0) {
      imageList.value = response.data.fileInfos.map((res) => {
        return { name: res.originalFilename, url: res.url, fileId: res.fileId };
      });
    }
    open.value = true;
    title.value = "修改节奏型";
  });
}

/** 提交按钮 */
function submitForm() {
  proxy.$refs["patternRef"].validate(valid => {
    if (valid) {
      const params = JSON.parse(JSON.stringify(form.value));  
      if (imageList.value.length > 0) {
        params.fileIds = imageList.value.map(i => i.fileId);
      }
      console.log("🚀 ~ submitForm ~ params:", params)
      if (form.value.patternId != null) {
        updatePattern(params).then(response => {
          proxy.$modal.msgSuccess("修改成功");
          open.value = false;
          getList();
        });
      } else {
        addPattern(params).then(response => {
          proxy.$modal.msgSuccess("新增成功");
          open.value = false;
          getList();
        });
      }
    }
  });
}

/** 节奏上传按钮操作 */
function handleUpdateJieZou(row) {
  const patternId = row.patternId;
  router.push({
    path: `/system/pattern/jiezou/${patternId}`
  });
}

/** 删除按钮操作 */
function handleDelete(row) {
  const _patternIds = row.patternId || ids.value;
  proxy.$modal.confirm('是否确认删除节奏型编号为"' + _patternIds + '"的数据项？').then(function () {
    return delPattern(_patternIds);
  }).then(() => {
    getList();
    proxy.$modal.msgSuccess("删除成功");
  }).catch(() => { });
}

getList();
</script>
